﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netra
{
    struct Position:IPosition
    {
        double x;
        double y;

        public Position Pos { get { return this; } }
        public string Name { get { return "(random position)"; } }

        public Position(double px, double py)
        {
            x = px;
            y = py;
        }
        
        public override string ToString()
        {
            return "(" + x.ToString() + "; " + y.ToString() + ")";
        }
        
        public double Distance(Position pos)
        {
            return Math.Sqrt(Math.Pow(x-pos.x,2)+Math.Pow(y-pos.y,2));
        }

        public double Norm
        {
            get
            {
                return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
            }
        }

        public void MoveTowards(Position aim,double speed)
        {
            if (Distance(aim) <= speed)
                this = aim;
            else
            {
                Position s = aim-this;
                s = speed / s.Norm * s;
                this = this + s;
            }
        }

        public static Position operator +(Position p1, Position p2)
        {
            return new Position(p1.x + p2.x, p1.y + p2.y);
        }

        public static Position operator -(Position p1, Position p2)
        {
            return new Position(p1.x - p2.x, p1.y - p2.y);
        }

        public static Position operator *(Position p, double c)
        {
            return new Position(c*p.x, c*p.y);
        }

        public static Position operator *(double c, Position p)
        {
            return p * c;
        }
        public static Position operator /(Position p, double c)
        {
            return p * (1/c);
        }

    }

    interface IPosition
    {
        Position Pos
        {
            get;
        }
        string Name
        {
            get;
        }
    }
}
